package com.appspot.chooserepository.client;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import com.appspot.chooserepository.shared.RepositoryUser;

public class RepositoryUsers implements Serializable {
	
	private ArrayList<String> usersList = new ArrayList<String>();
	private ArrayList<String> repositoriesList = new ArrayList<String>();
	private ArrayList<Date> lastUpdatedList = new ArrayList<Date>();
	private Date changeDate = null;
	
	public Date getChangeDate() {
		return changeDate;
	}

	public ArrayList<String> getUsersList() {
		return usersList;
	}

	public ArrayList<String> getRepositoriesList() {
		return repositoriesList;
	}

	public ArrayList<Date> getLastUpdatedList() {
		return lastUpdatedList;
	}

	public void setChangeDate(Date changeDate) {
		this.changeDate = changeDate;
	}
	
	public void setChangeDate(long changeDate) {
		setChangeDate(new Date(changeDate));
	}
	
	public Boolean isUpToDate(Date anotherDate) {
		if (changeDate == null || anotherDate == null)
			return false;
		return changeDate.equals(anotherDate);
	}
	
	public Boolean isUpToDate(long anotherDate) {
		return isUpToDate(new Date(anotherDate));
	}
	
	public void addRepositoryUser(RepositoryUser repositoryUser) {
		usersList.add(repositoryUser.getUser());
		repositoriesList.add(repositoryUser.getRepository());
		lastUpdatedList.add(repositoryUser.getLastUpdated());
	}
	
	public boolean removeRepositoryUser(RepositoryUser repositoryUser) {
		int index = getRepositoryUserIndex(repositoryUser);
		return removeRepositoryUser(index);
	}
	
	public boolean removeRepositoryUser(int repositoryUserIndex) {
		if (repositoryUserIndex == -1 || repositoryUserIndex >= size()) {
			return false;
		}
		usersList.remove(repositoryUserIndex);
		repositoriesList.remove(repositoryUserIndex);
		lastUpdatedList.remove(repositoryUserIndex);
		return true;
	}
	
	public RepositoryUser getRepositoryUser(int index) {
		return new RepositoryUser(usersList.get(index),repositoriesList.get(index),lastUpdatedList.get(index));
	}
	
	public int getRepositoryUserIndex(RepositoryUser repositoryUser) {
		// Because there can't be two entries of the same repository:
		int index = repositoriesList.indexOf(repositoryUser.getRepository());
		if (index == -1) {
			return -1;
		}
		if (repositoryUser.getUser().equals(usersList.get(index)) && 
				repositoryUser.getLastUpdated().equals(lastUpdatedList.get(index))) {
			return index;
		}	
		return -1;
	}
	
	public int size() {
		return usersList.size();
	}
	
	public String getUsedRepository(String user) {
		for (int i = 0; i < usersList.size(); i++) {
			if (usersList.get(i).equals(user)) 
				return repositoriesList.get(i);
		}
		return null;
	}

	public String getUsingUser(String repository) {
		for (int i = 0; i < repositoriesList.size(); i++) {
			if (repositoriesList.get(i).equals(repository)) 
				return usersList.get(i);
		}
		return null;
	}

}